package bb.lanxing.lib.devices.core;

import android.util.Log;
import bb.lanxing.lib.devices.api.ConnectionListener;
import bb.lanxing.lib.devices.api.DeviceStateListener;
import bb.lanxing.lib.devices.api.PeerDevice;
import bb.lanxing.lib.devices.api.SmartDevice;
import bb.lanxing.lib.devices.core.utils.ConnectionStateNotifier;
import bb.lanxing.lib.devices.core.utils.DeviceStateNotifier;
import java.io.Closeable;
import java.io.IOException;
import java.util.HashMap;

public abstract class AbstractPeerDevice implements PeerDevice {
    private SmartDevice mDevice;
    private final HashMap<String, Object> extraObj = new HashMap<>();
    private ConnectionStateNotifier mStateNotifier = new ConnectionStateNotifier();
    private DeviceStateNotifier mDeviceStateNotifier = new DeviceStateNotifier();

    public AbstractPeerDevice(SmartDevice smartDevice) {
        this.mDevice = smartDevice;
    }

    public SmartDevice getDevice() {
        return this.mDevice;
    }

    @Override
    public String getAddress() {
        SmartDevice smartDevice = this.mDevice;
        if (smartDevice != null) {
            return smartDevice.getAddress();
        }
        return null;
    }

    @Override
    public String getName() {
        SmartDevice smartDevice = this.mDevice;
        if (smartDevice != null) {
            return smartDevice.getName();
        }
        return null;
    }

    public int getFlags() {
        SmartDevice smartDevice = this.mDevice;
        if (smartDevice != null) {
            return smartDevice.getFlags();
        }
        return 0;
    }

    @Override
    public int getProtocol() {
        SmartDevice smartDevice = this.mDevice;
        if (smartDevice != null) {
            return smartDevice.getProtocol();
        }
        return 0;
    }

    @Override
    public int getType() {
        SmartDevice smartDevice = this.mDevice;
        if (smartDevice != null) {
            return smartDevice.getType();
        }
        return 0;
    }

    @Override
    public void registerConnectionListener(ConnectionListener connectionListener) {
        ConnectionStateNotifier connectionStateNotifier = this.mStateNotifier;
        if (connectionStateNotifier != null) {
            connectionStateNotifier.registerConnectionListener(connectionListener);
        }
    }

    @Override
    public void unregisterConnectionListener(ConnectionListener connectionListener) {
        ConnectionStateNotifier connectionStateNotifier = this.mStateNotifier;
        if (connectionStateNotifier != null) {
            connectionStateNotifier.unregisterConnectionListener(connectionListener);
        }
    }

    @Override
    public void registerStateListener(DeviceStateListener deviceStateListener) {
        DeviceStateNotifier deviceStateNotifier = this.mDeviceStateNotifier;
        if (deviceStateNotifier != null) {
            deviceStateNotifier.registerDeviceStateListener(deviceStateListener);
        }
    }

    @Override
    public void unregisterStateListener(DeviceStateListener deviceStateListener) {
        DeviceStateNotifier deviceStateNotifier = this.mDeviceStateNotifier;
        if (deviceStateNotifier != null) {
            deviceStateNotifier.unregisterDeviceStateListener(deviceStateListener);
        }
    }

    public void notifyStateChanged(int vState, int errCode) {
        ConnectionStateNotifier connectionStateNotifier = this.mStateNotifier;
        if (connectionStateNotifier != null) {
            connectionStateNotifier.notifyStateChanged(this.mDevice, vState, errCode);
        }
        if (vState == PeerDevice.STATE_DISCONNECTED) {
            clearObj();
        }
    }

    public void notifyDeviceStateChanged(int vState) {
        DeviceStateNotifier deviceStateNotifier = this.mDeviceStateNotifier;
        if (deviceStateNotifier != null) {
            deviceStateNotifier.notifyStateChanged(this.mDevice, vState);
        }
    }

    public void e(String str) {
        Log.d(getTag(), str);
    }

    public final void e(Throwable th) {
        e(Log.getStackTraceString(th));
    }

    public void d(String str) {
        Log.d(getTag(), str);
    }

    protected String getTag() {
        return getName();
    }

    protected void clearObj() {
        synchronized (this.extraObj) {
            for (Object obj : this.extraObj.values()) {
                if (obj instanceof Closeable) {
                    try {
                        ((Closeable) obj).close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    public <T> T putObj(String str, T t) {
        this.extraObj.put(str, t);
        return t;
    }

    public <T> T getObj(String str) {
        T t;
        HashMap<String, Object> hashMap = this.extraObj;
        if (hashMap == null) {
            return null;
        }
        synchronized (hashMap) {
            try {
                try {
                    t = (T) this.extraObj.get(str);
                } catch (Exception e) {
                    e.printStackTrace();
                    return null;
                }
            } catch (Throwable th) {
                throw th;
            }
        }
        return t;
    }
}
